#include "headfiles.h"

using namespace std;

#define CHECK_STACK_INDEX() \
	if (stack_num >= STACK_NUM) { \
		cout << "Incorrect stack index, " << stack_num << endl; \
		return false; \
	}


ThreeStack::ThreeStack() {
	stack_size = 100;
	array = (int *)malloc(sizeof(int) * stack_size * 3);
	if (array == NULL) {
		cout << "Allocate array error!" << endl;
	}
	memset(node_num, 0, sizeof(int) * STACK_NUM);
}


ThreeStack::~ThreeStack() {
	free(array);
}

bool ThreeStack::push(int stack_num, int val) {
	CHECK_STACK_INDEX();

	if (node_num[stack_num] >= stack_size) {
		cout << "Stack " << stack_num << " is full!" << endl;
		return false;
	}

	array[node_num[stack_num]] = val;
	++node_num[stack_num];

	return true;
}

bool ThreeStack::pop(int stack_num) {
	CHECK_STACK_INDEX();

	if (node_num[stack_num] <= 0) {
		cout << "Stack " << stack_num << " is empty!" << endl;
		return false;
	}

	--node_num[stack_num];

	return true;
}

int ThreeStack::top(int stack_num) {
	CHECK_STACK_INDEX();

	if (node_num[stack_num] <= 0) {
		cout << "Stack " << stack_num << " is empty!" << endl;
		return false;
	}

	return array[node_num[stack_num] - 1];
}

size_t ThreeStack::size(int stack_num) {
	CHECK_STACK_INDEX();

	return node_num[stack_num];
}
	
bool ThreeStack::empty(int stack_num) {
	CHECK_STACK_INDEX();
	
	if (node_num[stack_num] == 0) {
		return true;
	}
	else {
		return false;
	}
}

